/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain;

import java.awt.Point;
import java.awt.Shape;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import mapping.ClientMapper;
import network.Network;

/**
 *
 * @author Jasper
 */
public class DrawController {
    private ClientMapper clientMapper;
    private ServerSocket serverSocket;
    private Spel spel;
    private List<Point> points ;
    
    //TODO serversocket ook opnemen in constructor???
    public DrawController(ClientMapper clientMapper, Spel spel){
        this.clientMapper = clientMapper;
        this.spel = spel;
        serverSocket = null;
        points = new ArrayList<Point>();
         try{
             serverSocket = new ServerSocket(Network.PORT);
             run();
         }catch(IOException e){
            System.exit(1);
        }
    }   

    public void guessedWord(Socket link){
        try{
            PrintWriter output = new PrintWriter(link.getOutputStream());
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            
            String woord, naam;
            //woord --> server
            woord = input.readLine();
            
            //naam --> server
            naam = input.readLine();
            System.out.printf("%s heeft %s gegokt \n", naam, woord);
            
            //server --> resultaat
            if(woord.equalsIgnoreCase(spel.getWoord())){
                output.println("true");
                //RADER --> score +1
                Client client = clientMapper.getClient(naam);
                client.setScore(client.getScore() + 1);
                clientMapper.updateClient(client.getNaam(), client);
                
                //SPEL SLUITEN
                spel.setFinished(true);
            }else{
                output.println("false");
            }
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    public void setWord(Socket link){
        try{
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream())); 
            System.out.printf("Het woord van dit spel is: %s \n", input.readLine());
            //woord --> server
            spel.setWoord(input.readLine());
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    public void loadPoints(Socket link){
        try{
            OutputStream outputStream = link.getOutputStream();      
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(points);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    public void isFinished(Socket link){
        try{
            PrintWriter output = new PrintWriter(link.getOutputStream());
            //SERVER --> ANTWOORD
            output.println(spel.isFinished());
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    public void updateFrame(Socket link){
        try{            
            InputStream inputStream = link.getInputStream();      
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            points = (ArrayList<Point>) objectInputStream.readObject();
            objectInputStream.close();
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    private void run() {
        Socket link = null;
        try{
            link = serverSocket.accept();
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            String opdracht = input.readLine();
            switch(opdracht){
                case "guessWord": guessedWord(link);break;
                case "updateFrame": updateFrame(link);break;
                case "setWord":setWord(link);break;
                case "loadPoints":loadPoints(link);break;
                case "isFinished":isFinished(link);break;
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }
    }
    
}
